package com.hanma.dao.impl;

import com.hanma.bean.Manager;
import com.hanma.bean.User;
import com.hanma.dao.ManagerDao;
import com.hanma.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ManagerDaoImpl implements ManagerDao {
    @Override
    public Manager findByNameAndPassword(String username, String password) {
        Connection conn = DBUtil.open();
        String sql="select * from manager where username=? and password=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();
            while(rs.next()) {
                Integer id=rs.getInt("id");
                Integer pid=rs.getInt("pid");
                Integer grade=rs.getInt("grade");
                Manager manager = new Manager(id, pid, username, password,grade);
                return manager;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(rs, ps, conn);
        }
        return null;
    }

    @Override
    public int insert(Manager manager) {
        Connection conn = DBUtil.open();
        int num=0;
        String sql="insert into manager(username,pid,password)  values(?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, manager.getUsername());
            ps.setInt(2, manager.getPid());
            ps.setString(3, manager.getPassword());
            num=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(null, ps, conn);
        }
        return num;
    }

    @Override
    public List<Manager> findAll(Integer pid) {
        Connection conn = DBUtil.open();
        String sql="select * from manager where pid=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Manager> managerList = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, pid);
            rs = ps.executeQuery();
            while(rs.next()) {
                Integer id=rs.getInt("id");
                String username=rs.getString("username");
                String password=rs.getString("password");
                Integer grade=rs.getInt("grade");
                Manager manager = new Manager(id, pid, username, password,grade);
                managerList.add(manager);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(rs, ps, conn);
        }
        return managerList;
    }

    @Override
    public Manager findAllById(Integer id) {
        Connection conn = DBUtil.open();
        String sql="select * from manager where id=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while(rs.next()) {
                String username=rs.getString("username");
                String password=rs.getString("password");
                Integer pid=rs.getInt("pid");
                Integer grade=rs.getInt("grade");
                Manager manager = new Manager(id, pid, username, password,grade);
                return manager;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(rs, ps, conn);
        }
        return null;
    }
}
